package com.sinodata.bsm.cicp.collector.tuxedo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.SnmpConnect;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class TuxedoSnmpCollector extends Collector {

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String[] propMetas = getPropMeta();
        String oids = this.paramMap.get("OIDS");
        String[] pduOIDS = oids.split("[,]");
        String community = this.paramMap.get("Community");
        Map<Integer, String> resultMap = new HashMap<Integer, String>();
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[getPropMeta().length];

        try {
            resultMap = SnmpConnect.getValueBySnmp(ip, pduOIDS, community);
            for (int i = 0; i < pduOIDS.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(resultMap.get(i), propMetas[i]);
            }
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception e) {
            logger.error("Collect tuxedo error.", e);
            throw new CollectorException(e);
        }

        return values;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "OIDS", "Community", "matchNum" };
    }

    @Override
    public String[] getPropMeta() {
        int count = Integer.parseInt(this.paramMap.get("matchNum"));
        String[] propMetas = new String[count];
        for (int i = 1; i <= propMetas.length; i++) {
            propMetas[i - 1] = "$" + i;
        }
        return propMetas;
    }

}
